#include "StdAfx.h"
#include "SaveNewBagInfo.h"
#include "CmdQueue.h"
#include "DataBaseManage.h"
#include "LogWrapper.h"

CSaveNewBagInfo::CSaveNewBagInfo(void)
{
	m_pCmdQueue = new CCmdQueue;
	StartThread();
}

CSaveNewBagInfo::~CSaveNewBagInfo(void)
{
	WaitForExit(10);
	if (m_pCmdQueue)
	{
		delete m_pCmdQueue;
		m_pCmdQueue = NULL;
	}
}

void CSaveNewBagInfo::SaveNewBagInfoToDB( const wchar_t* const szPrn, const wchar_t* const szUserID, const wchar_t* szBID /*= NULL*/ )
{
	NewBagInfo* info = new NewBagInfo;
	if (szPrn)
	{
		wcscpy_s(info->szPrn, MAX_PRN_LENGTH, szPrn);
	}
	if (szUserID)
	{
		wcscpy_s(info->szUserID, MAX_USER_ID_LENGTH, szUserID);
	}
	if (szBID)
	{
		wcscpy_s(info->szBID, MAX_BID_LENGTH, szBID);
	}
	COMMAND* cmd  = new COMMAND;
	cmd->lpBuffer = (BYTE*)info;
	this->m_pCmdQueue->PushCommand(cmd);
}

DWORD CSaveNewBagInfo::ThreadMemberFunc()
{
	while(IsRepeat())
	{
		DWORD dwResult = WaitForSingleObject(this->m_pCmdQueue->CommandEvent(), INFINITE);
		if (dwResult == WAIT_OBJECT_0)
		{
			COMMAND* cmd		= this->m_pCmdQueue->PopCommand();
			NewBagInfo* info	= (NewBagInfo*)cmd->lpBuffer;

			CSingleton<CDataBaseManage>::Instance()->SaveNewBagInfoToDB(info->szPrn, info->szUserID);

			CSingleton<CLogWrapper>::Instance()->Log(TRACE_LOG, L"SaveNewBagInfoToDB thread: new baggage %s, %s.\n", CString(info->szPrn), CString(info->szUserID));			
				
			delete cmd; cmd	 = NULL;
			delete info; info= NULL;
		}
	}

	return 0;
}
